package com.waterdrop.wlsggk.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.waterdrop.wlsggk.mapper.InventoryMapper;
import com.waterdrop.wlsggk.mapper.TransferInventoryMapper;
import com.waterdrop.wlsggk.mapper.TransferReplenishMapper;
import com.waterdrop.wlsggk.pojo.Inventory;
import com.waterdrop.wlsggk.pojo.TransferInventory;
import com.waterdrop.wlsggk.pojo.TransferReplenish;
import com.waterdrop.wlsggk.service.ITransferReplenishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 中转仓库补货操作
 *
 * @author Naruto
 * @date 2023-10-24
 */
@Service
public class TransferReplenishServiceImpl implements ITransferReplenishService {

    @Autowired
    private TransferReplenishMapper transferReplenishMapper;

    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private TransferInventoryMapper transferInventoryMapper;

    /**
     * 查询补货
     *
     * @param replenishId
     * @return wlsggk
     */
    @Override
    public TransferReplenish selectTransferReplenishByReplenishId(Long replenishId) {
        return transferReplenishMapper.selectTransferReplenishByReplenishId(replenishId);
    }

    /**
     * 查询补货列表
     *
     * @param transferReplenish
     * @return wlsggk
     */
    @Override
    public List<TransferReplenish> selectTransferReplenishList(TransferReplenish transferReplenish) {
        return transferReplenishMapper.selectTransferReplenishList(transferReplenish);
    }

    /**
     * 新增补货记录
     * modify by Naruto 20231101
     * 补货将减少中央仓库库存
     * 将各个口味的补货量添加至中转仓库
     *
     * TODO 同时记录中央仓库和中转仓库历史记录信息
     *
     * @param transferReplenish
     * @return 结果
     */
    @Override
    public int insertTransferReplenish(TransferReplenish transferReplenish) {
        // 减少中央仓库库存
        Inventory inventory = new Inventory();
        BeanUtils.copyProperties(transferReplenish, inventory);
        inventoryMapper.reduceInventoryCount(inventory);
        // 增加中转仓库库存
        TransferInventory transferInventory = new TransferInventory();
        BeanUtils.copyProperties(transferReplenish, transferInventory);
        transferInventoryMapper.addTransferInventoryCount(transferInventory);
        // 添加补货记录
        return transferReplenishMapper.insertTransferReplenish(transferReplenish);
    }

    /**
     * 修改补货记录
     *
     * @param transferReplenish wlsggk
     * @return 结果
     */
    @Override
    public int updateTransferReplenish(TransferReplenish transferReplenish) {
        transferReplenish.setUpdateTime(DateUtils.getNowDate());
        return transferReplenishMapper.updateTransferReplenish(transferReplenish);
    }

    /**
     * 批量删除补货记录
     *
     * @param replenishIds 需要删除的wlsggk主键
     * @return 结果
     */
    @Override
    public int deleteTransferReplenishByReplenishIds(Long[] replenishIds) {
        return transferReplenishMapper.deleteTransferReplenishByReplenishIds(replenishIds);
    }

    /**
     * 删除补货信息
     *
     * @param replenishId wlsggk主键
     * @return 结果
     */
    @Override
    public int deleteTransferReplenishByReplenishId(Long replenishId) {
        return transferReplenishMapper.deleteTransferReplenishByReplenishId(replenishId);
    }
}
